package gui.小练习.打字游戏;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

/**
 * @Package gui.小练习.打字游戏
 * @Author shimmer
 * @Date 2020/8/5 11:24
 * @TODO
 */
// 游戏的面板
public class GameJPanel extends JPanel implements ActionListener {
    boolean isStart = false;
    //定时器，每隔一秒执行一次actionPerformed（）事件监听，this实现了监听方法
    Timer timer = new Timer(1000 / 60, this);//1000毫秒=1秒
    long score;
    int blood;
    boolean boom = false;//碰撞标志
    int playerX = 450;
    final int playerY = 600;
    String fx = "R";
    public static Random random = new Random();//随机数
    ArrayList<EnemyInfo> enemyList;// 敌机数组
    ArrayList<BulletInfo> bulletList;// 子弹数组
    Boolean isLaunch = false;
    Boolean isFail = false;
    long gameTime;
    int enemyTime;
    int enemyNum;

    public GameJPanel() {
        this.setBackground(new Color(94, 63, 107));
        init();
        this.setFocusable(true);//获取焦点事件
        //开启键盘监听
        addKeyListener(new GameKeyListener(this, enemyList));
        //开启定时器，让飞船一秒刷新一次
        timer.start();
    }

    public void init() {
        enemyList = new ArrayList<>();
        bulletList = new ArrayList<>();
        blood = 200;
        enemyTime = 5;
        enemyNum = 3;
        addEnemy();
    }

    // 绘制面板,游戏中所有东西都用这支画笔
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        //积分板
        g.setColor(Color.orange);
        g.setFont(new Font("黑体", Font.PLAIN, 25));
        g.drawString("得分:", 75, 55);
        g.drawString("生命:", 300, 55);
        g.drawString("时间:", 650, 55);
        g.setColor(Color.green);
        g.setFont(new Font("黑体", Font.PLAIN, 24));
        g.drawString(score + "", 150, 55);
        // g.drawRect(375, 37, 200, 20);
        g.draw3DRect(375, 37, 200, 20, false);
        g.fillRect(375, 37, blood, 20);
        g.drawString(gameTime / 60 + ":" + gameTime % 60, 725, 55);
        if (boom) {
            GameData.playerDamaged.paintIcon(this, g, playerX, playerY);

        } else {
            GameData.player.paintIcon(this, g, playerX, playerY);
        }
        for (EnemyInfo enemy : enemyList) {
            GameData.enemyShip.paintIcon(this, g, enemy.enemyX, enemy.enemyY);
            g.drawString((char) enemy.aim + "", enemy.enemyX + 44, enemy.enemyY - 10);
        }
        // 发射子弹
        g.setColor(Color.orange);
        if (isLaunch) {
            for (BulletInfo bullet : bulletList) {
                g.fillOval(bullet.bulletX, bullet.bulletY, 10, 10);
            }
        }
        //游戏状态
        if (!isStart) {
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));//设置字体
            g.drawString("按下空格，开始游戏", 320, 350);
        }
        if (isFail) {
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("游戏失败,请按空格继续", 325, 350);
        }
    }

    // 事件监听
    // 每隔一秒刷新一次
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && !isFail) {
            boom = false;
            gameTime++;
            if (gameTime % 60 == 0){
                if ((gameTime / 60) % enemyTime == 0) {// 每五秒
                    addEnemy();
                }
                if (gameTime/60==10){// 第二波
                    enemyNum++;
                }
            }

            for (EnemyInfo enemy : enemyList) {
                // 碰撞判断
                if (playerY >= enemy.getEnemyY() && playerY <= enemy.enemyY + 50) {
                    if (playerX >= enemy.getEnemyX() && playerX <= enemy.getEnemyX() + 100// 左
                            || playerX + 100 >= enemy.getEnemyX() && playerX <= enemy.getEnemyX() + 100) { // 右
                        boom = true;
                        // enemyList.remove(enemy);//不能在遍历时删除
                        enemy.setAlive(false);
                        blood -= 20;
                    }
                }
                // 出界判断
                if (enemy.getEnemyY() >= 800) {
                    enemy.setAlive(false);
                }
                // 敌机的移动
                enemy.enemyY += 1;
                if (enemy.ex) {
                    enemy.enemyX += 5;
                } else {
                    enemy.enemyX -= 5;
                }
                if (enemy.enemyX >= 700 && enemy.ex || enemy.enemyX <= 200 && !enemy.ex) {
                    enemy.ex = !enemy.ex;//反向
                }
            }

            // 子弹的移动
            if (isLaunch) {
                for (BulletInfo bullet : bulletList) {
                    int xLength = bullet.getEnemy().getEnemyX() + 50 - 5 - bullet.getBulletX();
                    int yLength = bullet.getEnemy().getEnemyY() + 10 - bullet.getBulletY();
                    int speedX = xLength > 0 ? 15 : -15;
                    bullet.bulletX += speedX;
                    bullet.bulletY += yLength / 10;
                    // 击中敌机
                    if (bullet.bulletX >= bullet.getEnemy().getEnemyX() && bullet.bulletX <= bullet.getEnemy().getEnemyX() + 100
                            && bullet.bulletY >= bullet.getEnemy().getEnemyY() && bullet.bulletY <= bullet.getEnemy().enemyY + 50) {
                        bullet.getEnemy().setAlive(false);
                        bullet.setAlive(false);
                        score += 50;
                    }
                }
            }

            // 删除敌机和子弹
            enemyList.removeIf(enemy -> !enemy.getAlive());
            bulletList.removeIf(bullet -> !bullet.isAlive());

            // 飞机的运动
            if ("R".equals(fx)) {
                playerX += 10;
            } else if ("L".equals(fx)) {
                playerX -= 10;
            }
            // 边界判断
            if (playerX >= 865) {
                fx = "L";
            } else if (playerX <= 35) {
                fx = "R";
            }

        }
        // 失败判定
        if (blood <= 0) {
            isFail = true;
        }

        repaint();
        //开启定时器
        timer.start();
    }

    // 增加敌机
    public void addEnemy() {
        for (int i = 0; i < enemyNum; i++) {
            EnemyInfo enemy = new EnemyInfo();
            enemyList.add(enemy);
        }
    }

}
